package com.designpattern.design.mediator;

import java.util.HashMap;

/**
 * 中介者模式
 * @author 15790
 *
 */
public class MediatorTest {
public static void main(String[] args) {
	//新建一个具体中介
	SmartHouseMediator mediator = new SmartHouseMediator();
	Alarm alarm = new Alarm(mediator);
	TV tv = new TV(mediator);
	Coffee coffee = new Coffee(mediator);
	Curton curton = new Curton(mediator);
	alarm.sendAlarm(0);
	coffee.finishCoffee();
	
}
}

interface SmartHouse{
	void sendMessage(int stateChange);
	//获取中介对象
	SmartMediator getMediator();
}

/**
 * 闹钟
 * @author 15790
 *
 */
class Alarm implements SmartHouse{

	SmartMediator mediator;
	String name;
	
	public Alarm(SmartMediator mediator) {
		super();
		this.name="Alarm";
		this.mediator = mediator;
		//将同事类注册到中介中
		mediator.register(name, this);
	}
	
	@Override
	public void sendMessage(int stateChange) {
		// TODO Auto-generated method stub
		mediator.getMessage(stateChange, this.getClass().getSimpleName());
	}

	@Override
	public SmartMediator getMediator() {
		// TODO Auto-generated method stub
		return mediator;
	}
	
	public void sendAlarm(int stateChange) {
		this.sendMessage(stateChange);
	}
	
}

class TV implements SmartHouse{

	SmartMediator mediator;
	String name;
	
	public TV(SmartMediator mediator) {
		super();
		this.name="TV";
		this.mediator = mediator;
		//将同事类注册到中介中
		mediator.register(name, this);
	}

	@Override
	public void sendMessage(int stateChange) {
		// TODO Auto-generated method stub
		mediator.getMessage(stateChange,this.getClass().getSimpleName());
	}

	@Override
	public SmartMediator getMediator() {
		// TODO Auto-generated method stub
		return mediator;
	}
	
	public void on() {
		System.out.println("开启电视");
	}
	
	public void off() {
		System.out.println("关掉电视");
	}
	
}

class Coffee implements SmartHouse{

	SmartMediator mediator;
	String name;
	
	public Coffee(SmartMediator mediator) {
		super();
		this.name="Coffee";
		this.mediator = mediator;
		//将同事类注册到中介中
		mediator.register(name, this);
	}
	
	@Override
	public void sendMessage(int stateChange) {
		// TODO Auto-generated method stub
		mediator.getMessage(stateChange,this.getClass().getSimpleName());
	}
	
	public void startMake() {
		System.out.println("开始制作咖啡");
	}
	
	public void finishCoffee() {
		System.out.println("咖啡制作完成");
		sendMessage(1);
	}
	
	@Override
	public SmartMediator getMediator() {
		// TODO Auto-generated method stub
		return mediator;
	}
	
}

class Curton implements SmartHouse{

	SmartMediator mediator;
	String name;
	
	
	public Curton(SmartMediator mediator) {
		this.name="Curton";
		this.mediator = mediator;
		//将同事类注册到中介中
		mediator.register(name, this);
	}

	@Override
	public void sendMessage(int stateChange) {
		// TODO Auto-generated method stub
		this.mediator.getMessage(stateChange, this.getClass().getSimpleName());
	}

	@Override
	public SmartMediator getMediator() {
		// TODO Auto-generated method stub
		return mediator;

	}
	
	public void on() {
		System.out.println("打开窗帘");
	}
	public void off() {
		System.out.println("关闭窗帘");
	}
}

interface SmartMediator{
	//获取消息 传入状态和同事名
	void getMessage(int statusChange,String colledgeName);
	//注册同事类到中介方法
	void register(String name,SmartHouse house);
}

class SmartHouseMediator implements SmartMediator{

	HashMap<String,SmartHouse> map;
	
	
	public SmartHouseMediator() {
		super();
		map=new HashMap<>();
	}
	@Override
	public void getMessage(int statusChange,String colledgeName){
		//闹钟响
		if(map.get(colledgeName) instanceof Alarm) {
			if(statusChange==0) {
				Curton curton = (Curton)map.get("Curton");
				curton.off();
				Coffee coffee =(Coffee) map.get("Coffee");
				coffee.startMake();
				TV tv = (TV)map.get("TV");
				tv.on();
			}else {
				Curton curton = (Curton)map.get("Curton");
				curton.on();
				TV tv = (TV)map.get("TV");
				tv.off();
			}
		}
		if(map.get(colledgeName) instanceof Coffee) {
			if(statusChange==1) {
				Curton curton = (Curton)map.get("Curton");
				curton.on();
			}
		}
	}
	@Override
	public void register(String name, SmartHouse house) {
		// TODO Auto-generated method stub
		map.put(name, house);
	}
	
}